using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace quickTask
{
    public partial class CategoryDialog : Form
    {
        public CategoryDialog(List<string> theCategoryList)
        {
            mCategoryList = theCategoryList;
            InitializeComponent();
        }

        private void CategoryDialog_Load(object sender, EventArgs e)
        {
            foreach(string str in mCategoryList)
            {
                categoryListBox.Items.Add(str);
            }
        }

        private void categoryNewTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                addButton_Click(this, EventArgs.Empty);
            }
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            if(categoryNewTextBox.Text.Trim() != "")
            {
                mCategoryList.Add(categoryNewTextBox.Text);
                categoryListBox.Items.Add(categoryNewTextBox.Text);
                categoryNewTextBox.Clear();
                mIsDirty = true;
            }
        }

        private void removeButton_Click(object sender, EventArgs e)
        {
            if (categoryListBox.SelectedItem != null)
            {
                if (MessageBox.Show(this, "Delete " + categoryListBox.SelectedItem + "?",
                    "Delete Category", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    == DialogResult.Yes)
                {
                    mCategoryList.RemoveAt(categoryListBox.SelectedIndex);
                    categoryListBox.Items.RemoveAt(categoryListBox.SelectedIndex);

                    mIsDirty = true;
                }
            }
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            (Owner as Form1).saveCategoryList();
            mIsDirty = false;
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            if (mIsDirty)
            {
                if (MessageBox.Show(this, "Save changes?", "Unsaved Changes",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    saveButton_Click(this, EventArgs.Empty);
                }
            }

            Close();
        }


        private List<string> mCategoryList;
        private bool mIsDirty = false;

    }
}